﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iuPlace.Core
{
    public static class Application
    {
        static object LockSessions = new object();

        #region Properties Statics
        private static Dictionary<string, Session> _Sessions = new Dictionary<string, Session>();
        public static IEnumerable<Session> Sessions
        {
            get
            {
                return _Sessions.Values;
            }
        }
        #endregion

        #region Methods Statics
        public static void Initialize()
        {
            using (var context = Repositories.Context.CreateSmartContainer())
            {
                context.Update(Repositories.SCModelEntities.Table.User, new { OnLine = false });
                context.Execute();
            }
        }

        public static Session NewSession(string identification)
        {
            Session newSession = new Session() { Identification = identification };
            lock (Application.LockSessions)
            {
                Application._Sessions.Add(identification, newSession);
            }
            return newSession;
        }

        public static void RemoveSession(string identification)
        {
            lock (Application.LockSessions)
            {
                if (Application._Sessions.ContainsKey(identification))
                {
                    Application._Sessions.Remove(identification);
                }
            }
        }
        #endregion
    }
}
